package cn.lisynet.user.service.impl;

import cn.hutool.core.util.IdUtil;
import cn.lisynet.user.entity.User;
import cn.lisynet.user.mapper.UserMapper;
import cn.lisynet.user.service.AddTaskService;
import cn.lisynet.user.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author lisy
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Resource
    private AddTaskService taskService;
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @GlobalTransactional
    @Override
    public void addUser(User u) throws Exception {
        //调用微服务
        String rs = taskService.addTask(IdUtil.fastUUID());
        if(!"success".equals(rs)){
            throw new Exception("远程调用添加任务失败");
        }
        //调用本地数据库
        String sql="insert into req_log (ip,user_id,browser) values (?,?,?)";
        jdbcTemplate.update(sql, "127.0.0.1",1,IdUtil.fastUUID());
        //调用微服务
        String rs2 = taskService.addContent(IdUtil.fastUUID());
        if(!"success".equals(rs)){
            throw new Exception("远程调用添加内容失败");
        }
        //调用本地数据库
        this.baseMapper.insert(u);
    }
}
